// shopPages/OrderList/OrderList.js
var sliderWidth = 25;
var app = getApp()
var list_i = []
var list_ii = []
var list_iii = []
var list_iv = []
var list_v = []
var list_vi = []
var list_vii = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [{
        name: "全部",
        text: "0"
      },
      {
        name: "待发货",
        text: '0'
      },
      {
        name: '已发货',
        text: '0'
      },
      {
        name: '待评价',
        text: '0'
      },
      {
        name: "已评价",
        text: '0'
      },
      {
        name: '分销订单',
        text: '0'
      },
      {
        name: '退换货',
        text: '0'
      }
    ],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    orderList_i: [],
    orderList_ii: [],
    orderList_ii: [],
    orderList_iv: [],
    orderList_v: [],
    orderList_vi: [],
    orderList_vii: [],
    page_i: 1,
    page_ii: 1,
    page_iii: 1,
    page_iv: 1,
    page_v: 1,
    page_vi: 1,
    page_vii: 1,
    more_status_i: false,
    more_status_ii: false,
    more_status_iii: false,
    more_status_iv: false,
    more_status_v: false,
    more_status_vi: false,
    more_status_vii: false,
    // 订单列表
    OrderList: [],
    distribution: [],
    hui0_show: true,
    hui1_show: true,
    hui2_show: true,
    top0_show: false,
    top1_show: false,
    top2_show: false,
    currentSelectTripType: 'one',
    scrollLeft11: 0,
    inputSearch:'',
    clear:false
  },
  onchange: function (event) {
    let that = this;
    var inputSearch = event.detail.value;
    that.setData({
      searchInput: inputSearch,
    })
    that.setData({
      clear: inputSearch ? true : false
    })
  },
  // 清空inpunt
  bindXS: function () {
    this.setData({
      searchInput: '',
      clear: false,
    })
  },
  //点击查看订单详情
  showOrder: function(e) {
    var id = e.currentTarget.dataset.order_id;
    var index = e.currentTarget.dataset.index;
    var orderlist = e.currentTarget.dataset.list;
    var is_scan = e.currentTarget.dataset.is_scan;
    if (!is_scan) {
      wx.navigateTo({
        url: '../../shopPages/houOrderDetail/houOrderDetail?id=' + id + '&idx=' + index + '&orderlist=' + orderlist,
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  binddistr: function(e) {
    wx.navigateTo({
      url: '../distr/distr?order_id=' + e.currentTarget.dataset.order_id,
    })
  },
  onLoad: function(options) {
    var that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    list_i = []
    list_ii = []
    list_iii = []
    list_iv = []
    list_v = []
    list_vi = []
    list_vii = []
    that.get_orders(0, 0);
  },
  tabClick: function(e) {
    var that = this
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    //判断当前滚动超过一屏时，设置tab标题滚动条。
    if (e.currentTarget.id > 3) {
      that.setData({
        scrollLeft11: 300
      })
    } else {
      that.setData({
        scrollLeft11: 0
      })
    }
    if (e.currentTarget.id == 0 && (that.data.more_status_i == true || (that.data.more_status_i == false && that.data.page_i == 1))) {
      that.get_orders(e.currentTarget.id, 0);

    } else if (e.currentTarget.id == 1 && (that.data.more_status_ii == true || (that.data.more_status_ii == false && that.data.page_ii == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    } else if (e.currentTarget.id == 2 && (that.data.more_status_iii == true || (that.data.more_status_iii == false && that.data.page_iii == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    } else if (e.currentTarget.id == 3 && (that.data.more_status_iv == true || (that.data.more_status_iv == false && that.data.page_iv == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    } else if (e.currentTarget.id == 4 && (that.data.more_status_v == true || (that.data.more_status_v == false && that.data.page_v == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    } else if (e.currentTarget.id == 5 && (that.data.more_status_vi == true || (that.data.more_status_vi == false && that.data.page_vi == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    } else if (e.currentTarget.id == 6 && (that.data.more_status_vii == true || (that.data.more_status_vii == false && that.data.page_vii == 1))) {
      that.get_orders(e.currentTarget.id, 0);
    }

  },
  get_orders: function(activeIndex,state) {
    var that = this;
    var url2 = '/manager/seller_index';
    var param = {};

    param.type = activeIndex;
    param.keywords = that.data.searchInput;
    param.pagesize = 10;
    param.mg_id = app.globalData.userInfo.mg_id;
    param.uid = app.globalData.userInfo.id;
    wx.showLoading({
      title: '玩命加载中',
    })
    var more_status = false;
    var page = 1;
    var list = [];
    if (activeIndex == 0) {
      more_status = that.data.more_status_i;
      page = that.data.page_i;
      list = list_i;
    } else if (activeIndex == 1) {
      more_status = that.data.more_status_ii;
      page = that.data.page_ii;
      list = list_ii;
    } else if (activeIndex == 2) {
      more_status = that.data.more_status_iii;
      page = that.data.page_iii;
      list = list_iii;
    } else if (activeIndex == 3) {
      more_status = that.data.more_status_iv;
      page = that.data.page_iv;
      list = list_iv;
    } else if (activeIndex == 4) {
      more_status = that.data.more_status_v;
      page = that.data.page_v;
      list = list_v;
    } else if (activeIndex == 5) {
      more_status = that.data.more_status_vi;
      page = that.data.page_vi;
      list = list_vi;
    } else if (activeIndex == 6) {
      more_status = that.data.more_status_vii;
      page = that.data.page_vii;
      list = list_vii;
    }
    if(state>0){
      page = 1;
    }
    param.pageno = page;
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function(res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        more_status = false;
        if (activeIndex == 0) {
          that.setData({
            orderList_i: []
          })
        } else if (activeIndex == 1) {
          that.setData({
            orderList_ii: []
          })
        } else if (activeIndex == 2) {
          that.setData({
            orderList_iii: []
          })
        } else if (activeIndex == 3) {
          that.setData({
            orderList_iv: []
          })
        } else if (activeIndex == 4) {
          that.setData({
            orderList_v: []
          })
        } else if (activeIndex == 5) {
          that.setData({
            orderList_vi: []
          })
        } else if (activeIndex == 6) {
          that.setData({
            orderList_vii: []
          })
        }

      } else {
        var orderList = res.data;

        var o_list = list;

        if (page == 1) {
          o_list = [];
          if (activeIndex == 0) {
            that.setData({
              orderList_i: []
            })
          } else if (activeIndex == 1) {
            that.setData({
              orderList_ii: []
            })
          } else if (activeIndex == 2) {
            that.setData({
              orderList_iii: []
            })
          } else if (activeIndex == 3) {
            that.setData({
              orderList_iv: []
            })
          } else if (activeIndex == 4) {
            that.setData({
              orderList_v: []
            })
          } else if (activeIndex == 5) {
            that.setData({
              orderList_vi: []
            })
          } else if (activeIndex == 6) {
            that.setData({
              orderList_vii: []
            })
          }
        }
        var count = 0;
        for (let i in orderList) {
          o_list.push(orderList[i]);
          count++;
        }

        if (count < 10) {
          more_status = false;
        } else {
          more_status = true;

          page += 1;
          if (activeIndex == 0) {
            that.setData({
              page_i: page
            })
          } else if (activeIndex == 1) {
            that.setData({
              page_ii: page
            })
          } else if (activeIndex == 2) {
            that.setData({
              page_iii: page
            })
          } else if (activeIndex == 3) {
            that.setData({
              page_iv: page
            })
          } else if (activeIndex == 4) {
            that.setData({
              page_v: page
            })
          } else if (activeIndex == 5) {
            that.setData({
              page_vi: page
            })
          } else if (activeIndex == 6) {
            that.setData({
              page_vii: page
            })
          }
        }

        if (activeIndex == 0) {
          list_i = o_list;
          var str = 'tabs[0].text';
          var str1 = 'tabs[1].text';
          var str2 = 'tabs[2].text';
          var str3 = 'tabs[3].text';
          var str4 = 'tabs[4].text';
          var str5 = 'tabs[5].text';
          var str6 = 'tabs[6].text';
          that.setData({
            orderList_i: o_list,
            more_status_i: more_status,
          })
          if (res.count.length > 0) {
            that.setData({
              [str]: res.count[0],
              [str1]: res.count[1],
              [str2]: res.count[2],
              [str3]: res.count[3],
              [str4]: res.count[4],
              [str5]: res.count[5],
              [str6]: res.count[6],
            })
          }
        } else if (activeIndex == 1) {
          list_ii = o_list;

          that.setData({
            orderList_ii: o_list,
            more_status_ii: more_status,
          })

        } else if (activeIndex == 2) {
          list_iii = o_list;

          that.setData({
            orderList_iii: o_list,
            more_status_iii: more_status,
          })

        } else if (activeIndex == 3) {
          list_iv = o_list;

          that.setData({
            orderList_iv: o_list,
            more_status_iv: more_status,
          })

        } else if (activeIndex == 4) {
          list_v = o_list;

          that.setData({
            orderList_v: o_list,
            more_status_v: more_status,
          })

        } else if (activeIndex == 5) {
          list_vi = o_list;

          that.setData({
            orderList_vi: o_list,
            more_status_vi: more_status,
          })

        } else if (activeIndex == 6) {
          list_vii = o_list;

          that.setData({
            orderList_vii: o_list,
            more_status_vii: more_status,
          })

        }


      }
      wx.hideLoading();
    });
  },
  searchSubmitFn: function (e) {
    var that = this
    var inputxs = that.data.inputxs

    that.setData({
      inputxs: searchRecord ? false : true
    })
    var searchInput = that.data.searchInput
    searchInput = searchInput.replace(/\s+/g, '');
    var searchRecord = that.data.searchRecord
    that.get_orders(that.data.activeIndex, 1);
  },
  selectedZH: function() {
    // if ((that.data.more_status_vi == true || (that.data.more_status_vi == false && that.data.page_vi == 1))) {
    //   that.get_orders(e.currentTarget.id);
    // }else{

    // }
  },
  sortBy: function(attr, rev) {
    //第二个参数没有传递 默认升序排列
    if (rev == undefined) {
      rev = 1;
    } else {
      rev = (rev) ? 1 : -1;
    }

    return function(a, b) {
      a = a[attr];
      b = b[attr];
      if (a < b) {
        return rev * -1;
      }
      if (a > b) {
        return rev * 1;
      }
      return 0;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var activeIndex = that.data.activeIndex;

    if (activeIndex == 0 && that.data.more_status_i == true) {
      that.get_orders(activeIndex, 0);

    } else if (activeIndex == 1 && that.data.more_status_ii == true) {
      that.get_orders(activeIndex, 0);
    } else if (activeIndex == 2 && that.data.more_status_iii == true) {
      that.get_orders(activeIndex, 0);
    } else if (activeIndex == 3 && that.data.more_status_iv == true) {
      that.get_orders(activeIndex, 0);
    } else if (activeIndex == 4 && that.data.more_status_v == true) {
      that.get_orders(activeIndex, 0);
    } else if (activeIndex == 5 && that.data.more_status_vi == true) {
      that.get_orders(activeIndex, 0);
    } else if (activeIndex == 6 && that.data.more_status_vii == true) {
      that.get_orders(activeIndex, 0);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})